package kz.asoft;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import kz.asoft.am.common.RisksAppModule;
import kz.asoft.utils.ADFUtils;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.component.rich.nav.RichCommandMenuItem;

import oracle.jbo.ViewObject;
import oracle.jbo.domain.Number;
import oracle.jbo.uicli.binding.JUCtrlValueBinding;

public class CtrlObjGroupManageBean {
    private Boolean isSelected;
    private Number selectGroupId;
    
    public CtrlObjGroupManageBean() {
    }
    
    /**
     * Навигация по tab
     * @param actionEvent
     */
    public void getViewByGroup(ActionEvent actionEvent) {
        //получаем ID группы
        RichCommandMenuItem tab = (RichCommandMenuItem)actionEvent.getSource();
        Number ctrlObjGroupId = (Number)tab.getAttributes().get("groupId");
        selectGroupId = ctrlObjGroupId;
        DCIteratorBinding dcIb = ADFUtils.findIterator("SCtrlObjGroupsView1Iterator");
        dcIb.setCurrentRowWithKeyValue(ctrlObjGroupId.toString());
        
        RisksAppModule appModule = (RisksAppModule) ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        ViewObject vo = appModule.findViewObject("SCtrlObjsView1");
        vo.setWhereClause(null);
        vo.executeQuery();
    }


    public void setIsSelected(Boolean isSelected) {
        this.isSelected = isSelected;
    }

    public Boolean getIsSelected() {
        DCIteratorBinding dcIb = ADFUtils.findIterator("SCtrlObjGroupsView1Iterator");
        FacesContext fctx = FacesContext.getCurrentInstance();
        ELContext elctx = fctx.getELContext();
        ExpressionFactory efactory = fctx.getApplication().getExpressionFactory();
        ValueExpression ve = efactory.createValueExpression(elctx, "#{row}", Object.class);
        JUCtrlValueBinding row = (JUCtrlValueBinding)ve.getValue(elctx);
        Number iteratedRow = (Number) row.getAttribute("Id");
        isSelected = iteratedRow.equals(dcIb.getCurrentRow().getAttribute("Id"));
        return isSelected;
    }
}
